<?php

namespace App\Services\My;

use App\Dao\AppUpgradeDao;
use App\Dao\ClosenessDao;
use App\Dao\FollowActionDao;
use App\Dao\UserDao;
use function Hyperf\Translation\trans;

class VisitorService
{

    /**
     * Note - 个人中心-最近来访(最近7天)
     * DateTime 2024/8/8 16:15
     * User Admin
     * @param mixed $userInfo
     * @param array $params
     */
    public function list(mixed $userInfo, array $params)
    {
        $uid = $userInfo['id'];
        $type = $params['type'];
        $page = $params['page'] ?? 1;
        $pagesize = $params['pagesize'] ?? 12;
        $language = $params['language'];
        if ((new AppUpgradeDao())->isHideMaleChatCost(1, $params['version_code'], $params['channel'], $userInfo['last_login_ip'], $uid)) {
            // Tool::JSON(\CodeData::SUCCESS, array('result' => 1, 'msg' => [], 'visit_time' => time()), '');
            return formatData(array('result' => 1, 'msg' => [], 'visit_time' => time()));
        }

        $followAction = new FollowActionDao();
        // $myvisitors = $type == 1 ? $followAction->showMyVisitors($uid, $page, $pagesize) : $followAction->showMyCheck($uid, $page, $pagesize);
        if ($type == 1) {
            $myvisitors = $followAction->showMyVisitors($uid, $page, $pagesize, $userInfo['sex']);
        } else {
            $myvisitors = $followAction->showMyCheck($uid, $page, $pagesize);
        }

        $visitorsUid = array_column($myvisitors['data'], 'id');

        $tease_arr = [];
        $closeness = [];
        if (!empty($uids)) {
            // 用户打招呼记录
            $tease_arr = (new UserDao())->readTease($visitorsUid, $uid);
            $closeness = ClosenessDao::getMyCloseness($uid, $userInfo['sex'], $visitorsUid);
        }

        $sloganArr = trans('slogan', [], $language);
        $informationArr = trans('information', [], $language);
        foreach ($myvisitors['data'] as &$v) {
            if (empty($v['signature'])) {
                $v['signature'] = $sloganArr[array_rand($sloganArr)];
            }
            $v['is_tease'] = isset($tease_arr[$v['id']]) ? 1 : 0;// 是否打招呼:1-是,0-否
            if (isset($closeness[$v['id']]) && $closeness[$v['id']] > 0) {
                // 亲密度分数大于0，则已打招呼
                $v['is_tease'] = 1;
            }

            $v['ext'] = json_decode($v['ext'], true);
            $job = $v['ext']['job'] ?? '';
            if (!empty($job)) {
                $v['ext']['job'] = $informationArr['job'][$job]['name'] ?? '';
            } else {
                $v['ext']['job'] = '';
            }
        }
        // empty($myvisitors['data']) && $myvisitors['data'] = array();
        // empty($myvisitors['total']) && $myvisitors['total'] = 0;
        // Tool::JSON(\CodeData::SUCCESS, array('result' => 1, 'msg' => $myvisitors, 'visit_time' => time()), '');
        return formatData(array('result' => 1, 'msg' => $myvisitors, 'visit_time' => time()));
    }
}